package com.qf.controller;

import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping("/addCart")
    public String addCart(HttpServletRequest req){
        //获取参数列表
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        int number = Integer.parseInt(req.getParameter("number"));

        //session中获取用户信息
        User user = (User) req.getSession().getAttribute("user");

        Cart cart = cartService.findCartByUIdAndGoodsId(user.getId(), goodsId);

        Goods goods = goodsService.findById(goodsId);
        //获取商品价格
        double price = goods.getPrice();

        //如果购物车不存在，则创建
        if (cart == null){
            //创建购物车对象
            cart = new Cart();
            //创建属性
            cart.setId(user.getId());
            cart.setpId(goodsId);
            cart.setNum(number);
            cart.setMoney(number * price);
            //更新购物车
            cartService.insert(cart);
        }else {

            cart.setNum(cart.getNum() + number);

            cart.setMoney(cart.getNum() * price);
            //更新购物车
            cartService.update(cart);
        }

        //不需要存储到域，使用重定向
        return "redirect:/cartSuccess";
    }

    @RequestMapping("/getCart")
    public String getCart(HttpServletRequest req){

        User user = (User) req.getSession().getAttribute("user");

        List<Cart> carts = cartService.getCarts(user.getId());

        req.setAttribute("carts",carts);

        return "forward:/cart";
    }

    @RequestMapping("/clearCart")
    public void clearCart(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("user");
        cartService.clearCart(user.getId());
    }

    @RequestMapping("/addCartAjax")
    public void addCartAjax(HttpServletRequest req,HttpServletResponse resp){
        //获取参数列表
        int goodsId = Integer.parseInt(req.getParameter("goodsId"));
        int number = Integer.parseInt(req.getParameter("number"));

        //session中获取用户信息
        User user = (User) req.getSession().getAttribute("user");

        //如果number==0，删除该商品对应的购物车
        if (number == 0){
            cartService.deleteCart(user.getId(),goodsId);
        }else {

            Cart cart = cartService.findCartByUIdAndGoodsId(user.getId(), goodsId);
            //商品id
            //查询商品信息
            Goods goods = goodsService.findById(goodsId);

            //如果购物车不存在，则创建
            if (cart == null){
                //创建购物车对象
                cart = new Cart();
                //创建属性
                cart.setId(user.getId());
                cart.setpId(goodsId);
                cart.setNum(number);
                cart.setMoney(goods.getPrice());
                //更新购物车
                cartService.insert(cart);
            }else {
                //如果存在，则修改商品属性
                cart.setNum(cart.getNum() + number);
                //修改总价
                cart.setMoney(cart.getNum() * goods.getPrice());
                //更新购物车
                cartService.update(cart);
            }
        }
    }

}
